//
//  GraphView.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/28.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "GraphView.h"
#import "NSString+StringSize.h"
#import "GraphTipView.h"

@interface GraphView()
@property (nonatomic,strong)CAShapeLayer *shapeLayer;
@property (nonatomic,strong)CAShapeLayer *roundLayer;
@property (nonatomic,strong)UIView *baseView;
@property (nonatomic,strong)UILabel *detailLabel;
@property (nonatomic,strong)GraphTipView *tipView;
@property (nonatomic,assign)NSInteger lineIndex;
@end

@implementation GraphView
- (GraphTipView *)tipView
{
    if (!_tipView) {
        _tipView = [GraphTipView new];
        _tipView.frame = CGRectMake(-100, -100, 120, 50);
        [self addSubview:_tipView];
    }
    return _tipView;
}
- (UIView *)baseView
{
    if(!_baseView){
        _baseView = [UIView new];
        _baseView.backgroundColor = [UIColor grayColor];
        _baseView.alpha = 0.4;
        [self addSubview:_baseView];
    }
    return _baseView;
}
- (UILabel *)detailLabel
{
    if (!_detailLabel) {
        _detailLabel = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:left];
        [self addSubview:_detailLabel];
    }
    return _detailLabel;
}
- (CAShapeLayer *)shapeLayer
{
    if (!_shapeLayer) {
        _shapeLayer = [CAShapeLayer layer];
        [self.layer addSublayer:_shapeLayer];
    }
    return _shapeLayer;
}
- (CAShapeLayer *)roundLayer
{
    if (!_roundLayer) {
        _roundLayer = [CAShapeLayer layer];
        [self.layer addSublayer:_roundLayer];
    }
    return _roundLayer;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        _lineIndex = -1;
        [self setUpUI];
    }
    return self;
}

- (void)setPointArray:(NSArray *)pointArray
{
    _pointArray = pointArray;
}
- (void)setLineColor:(UIColor *)lineColor
{
    _lineColor = lineColor;
}
- (void)setUpUI
{
    UIView *dimView = [[UIView alloc] initWithFrame:self.bounds];
    dimView.backgroundColor = [UIColor whiteColor];
    [self addSubview:dimView];
    [UIView animateWithDuration:1.0f animations:^{
        dimView.frame = CGRectMake(self.bounds.size.width, self.bounds.size.height, 0, 0);
    }];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [touches.anyObject locationInView:self];
    [self drawLineWithPoint:point];
}
#define POINT(_INDEX_) [(NSValue *)[_pointArray objectAtIndex:_INDEX_] CGPointValue]


- (void)drawLineWithPoint:(CGPoint)point
{
    int x = ceil(point.x);
    NSInteger index = 0;
    if (x%60<30)
    {
        index = x/60+1;
    }else{
        index = x/60 + 2;
    }
    if (index>=_pointArray.count-1) {
        index = _pointArray.count-1;
    }
    NSLog(@"%ld",(long)index);
    CGPoint circlePoint = POINT(index);
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:CGPointMake(circlePoint.x, self.bounds.size.height-1)];
    [bezierPath addLineToPoint:CGPointMake(circlePoint.x, circlePoint.y-20)];
    [bezierPath addLineToPoint:CGPointMake(circlePoint.x, circlePoint.y)];
    [bezierPath addLineToPoint:CGPointMake(circlePoint.x+20, circlePoint.y)];
    [bezierPath addLineToPoint:CGPointMake(0, circlePoint.y)];
    self.shapeLayer.lineWidth = 4.f/3;
    _shapeLayer.strokeColor = _lineColor.CGColor;
    _shapeLayer.path = bezierPath.CGPath;
    _shapeLayer.fillColor = nil;
    
    UIBezierPath *roundPath = [UIBezierPath bezierPath];
    [roundPath addArcWithCenter:circlePoint radius:4 startAngle:0 endAngle:M_PI*2 clockwise:YES];
    self.roundLayer.lineWidth = 4.f/3;
    _roundLayer.strokeColor = _lineColor.CGColor;
    _roundLayer.path = roundPath.CGPath;
    _roundLayer.fillColor = _lineColor.CGColor;
    
    if (index>=_dataArray.count) {
        index = _dataArray.count;
    }
    NSDictionary *dict = _dataArray[index-1];
    NSString *str = [NSString stringWithFormat:@"%@",dict[@"value"]];;
//    self.detailLabel.text = str;
//    CGSize size = [str sizeWithLabelWidth:100 font:[UIFont systemFontOfSize:NormalFontSize]];
//    if (index >= _dataArray.count-1) {
//         self.baseView.frame = CGRectMake(circlePoint.x, circlePoint.y-40, size.width, size.height);
//    }else{
//         self.baseView.frame = CGRectMake(circlePoint.x+size.width*0.5, circlePoint.y-size.height, size.width, size.height);
//    }
   
    self.tipView.titleStr1 = _tipTitle;
    self.tipView.numStr2 = dict[@"key"];
    self.tipView.titleStr2 = @"日期";
    self.tipView.detailStr = str;
    
    if (_lineIndex == index) {
        _lineIndex = -1;
        self.tipView.hidden = YES;
    }else{
        self.tipView.hidden = NO;
        _lineIndex = index;
    }
    
    
    if (index == 1) {
        self.tipView.center = CGPointMake(circlePoint.x+60, circlePoint.y-40);
    }else if(index == _dataArray.count){
        self.tipView.center = CGPointMake(circlePoint.x-60, circlePoint.y-40);
    }else{
        self.tipView.center = CGPointMake(circlePoint.x, circlePoint.y-40);
    }
    
}
- (void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;
}

@end
